package com.ruoyi.petSys.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.petSys.domain.PromAdoption;
import com.ruoyi.petSys.entity.vo.ApplyAdoptionVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.petSys.mapper.ApplyAdoptionMapper;
import com.ruoyi.petSys.domain.ApplyAdoption;
import com.ruoyi.petSys.service.IApplyAdoptionService;

/**
 * 申请领养Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-08
 */
@Service
public class ApplyAdoptionServiceImpl implements IApplyAdoptionService 
{
    @Autowired
    private ApplyAdoptionMapper applyAdoptionMapper;
    @Autowired
    private PromAdoptionServiceImpl promAdoptionService;

    /**
     * 查询申请领养
     * 
     * @param id 申请领养主键
     * @return 申请领养
     */
    @Override
    public ApplyAdoptionVo selectApplyAdoptionById(Long id)
    {
        return applyAdoptionMapper.selectApplyAdoptionById(id);
    }

    /**
     * 查询申请领养列表
     * 
     * @param applyAdoption 申请领养
     * @return 申请领养
     */
    @Override
    public List<ApplyAdoptionVo> selectApplyAdoptionList(ApplyAdoption applyAdoption)
    {

        List<ApplyAdoptionVo> applyAdoptionVos = applyAdoptionMapper.selectApplyAdoptionList(applyAdoption);

        return applyAdoptionVos.stream().peek(applyAdoptionVo -> {
            if (ObjectUtil.isEmpty(applyAdoptionVo.getIsConsent()) || applyAdoptionVo.getIsConsent() == 1) {
                applyAdoptionVo.setConsentStatus("是");
            } else if(ObjectUtil.isEmpty(applyAdoptionVo.getIsConsent()) || applyAdoptionVo.getIsConsent() == 2){
                applyAdoptionVo.setConsentStatus("拒绝");
            }else {
                applyAdoptionVo.setConsentStatus("否");
            }
        }).collect(Collectors.toList());
    }

    /**
     * 新增申请领养
     * 
     * @param applyAdoption 申请领养
     * @return 结果
     */
    @Override
    public int insertApplyAdoption(ApplyAdoption applyAdoption)
    {
        applyAdoption.setCreateTime(DateUtils.getNowDate());
        applyAdoption.setIsConsent(0);
        return applyAdoptionMapper.insertApplyAdoption(applyAdoption);
    }

    /**
     * 修改申请领养
     * 
     * @param applyAdoption 申请领养
     * @return 结果
     */
    @Override
    public int updateApplyAdoption(ApplyAdoption applyAdoption)
    {
        applyAdoption.setUpdateTime(DateUtils.getNowDate());
        if( new Integer(1).equals(applyAdoption.getIsConsent()))
        {
            PromAdoption promAdoption = new PromAdoption();
            promAdoption.setId(applyAdoption.getAdoptionId());
            promAdoption.setIsAdoption(applyAdoption.getIsConsent());
            promAdoptionService.updatePromAdoption(promAdoption);
        }
        return applyAdoptionMapper.updateApplyAdoption(applyAdoption);

    }

    /**
     * 批量删除申请领养
     * 
     * @param ids 需要删除的申请领养主键
     * @return 结果
     */
    @Override
    public int deleteApplyAdoptionByIds(Long[] ids)
    {
        return applyAdoptionMapper.deleteApplyAdoptionByIds(ids);
    }

    /**
     * 删除申请领养信息
     * 
     * @param id 申请领养主键
     * @return 结果
     */
    @Override
    public int deleteApplyAdoptionById(Long id)
    {
        return applyAdoptionMapper.deleteApplyAdoptionById(id);
    }
}
